package com.test.Service.Impl;

import com.test.Mapper.BorrowMapper;
import com.test.Service.BorrowService;
import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Resource
    BorrowMapper borrowMapper;

    @Override
    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {

            List<Borrow> borrows = borrowMapper.getBorrowsByUid(uid);

            RestTemplate template = new RestTemplate();
            User user = template.getForObject("http://localhost:8301/user/" + uid, User.class);
            List<Book> books = borrows.stream().map(b ->
                            template.getForObject("http://localhost:8101/book/" + b.getBid(), Book.class)
                    )
                    .collect(Collectors.toList());
            return new UserBorrowDetail(user, books);
        }
    }

